//
//  TYZNetworkPromptView.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/10/18.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "TYZNetworkPromptView.h"

@interface TYZNetworkPromptView ()<UITextViewDelegate>
/**
 *  重试
 */
@property(nonatomic) void(^againBlock)(void);
@end

@implementation TYZNetworkPromptView

+ (instancetype)networkCutOffViewWithAgain:(void(^)(void))again {
    UIWindow *window = [TYZApplication sharedApplication].keyWindow;
    TYZNetworkPromptView *view = [window viewWithTag:TAG_BASEVIEW_WindowNetworkCutOffView];
    if (view == nil) {
        view = [[TYZNetworkPromptView alloc] init];
        view.tag = TAG_BASEVIEW_WindowNetworkCutOffView;
        [window addSubview:view];
        view.frame = CGRectMake(0, DE_NavigationHeight, kWindowWidth, 40);
    }
    view.againBlock = again;
    return view;
}
- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = TYZLightMMColor;
        [self protocolIsSelect];
    }
    return self;
}
- (void)setupUI {
    NSString *str1 = [@"网络开了小差，未成功获取客户信息，点击" S];
    NSString *str2 = [@"加载" S];
    NSString *str3 = [@"重试" S];
    TYZLabel *label1 = [TYZLabel labelWithText:str1];
    TYZButton *button2 = [[TYZButton alloc] init];
    [button2 setTitle:str2 forState:UIControlStateNormal];
    TYZLabel *label3 = [TYZLabel labelWithText:str3];
    [self addSubview:label1];
    [self addSubview:button2];
    [self addSubview:label3];
    NSString *str = [NSString stringWithFormat:@"%@%@%@", str1, str2, str3];
    UIFont *font = [UIFont systemFontOfSize:CoreInfo.fontSize - 2];
    CGSize size = [str getSizeWithFont:font];
    label1.font = font;
    button2.titleLabel.font = font;
    label3.font = font;
    label1.textColor = TYZLightColor;
    [button2 setTitleColor:TYZBlueColor forState:UIControlStateNormal];
    label3.textColor = TYZLightColor;
    [button2 addTarget:self action:@selector(didClickAgainButton) forControlEvents:UIControlEventTouchUpInside];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.left.equalTo(self).offset((kWindowWidth - size.width) * 0.5);
    }];
    [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(label1);
        make.left.equalTo(label1.mas_right).offset(-45);
    }];
    [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(label1);
        make.left.equalTo(button2.mas_right).offset(-2);
    }];
}
/**
 隐藏网络断开的无数据提示语
 */
+ (void)hiddennetworkCutOffView {
    TYZNetworkPromptView *view = [[TYZApplication sharedApplication].keyWindow viewWithTag:TAG_BASEVIEW_WindowNetworkCutOffView];
    if (view) {
        [view removeFromSuperview];
    }
}
- (void)didClickAgainButton {
    [self removeFromSuperview];
    if (self.againBlock) {
        self.againBlock();
    }
}
- (void)protocolIsSelect {
    CGFloat font = CoreInfo.fontSize;
    NSString *str = [@"网络开了小差，未成功获取信息，点击加载重试" S];
    TYZTextView *textView = [[TYZTextView alloc] init];
    textView.backgroundColor = TYZClearColor;
    [self addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.width.mas_equalTo([str getSizeWithFont:[UIFont systemFontOfSize:font + 0.5]].width);
        make.height.mas_equalTo([str getSizeWithFont:[UIFont systemFontOfSize:font + 1]].height + 5);
        make.centerY.equalTo(self).offset(-5);
    }];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
    [attributedString addAttribute:NSLinkAttributeName
                             value:@"LoadingAgain://"
                             range:[[attributedString string] rangeOfString:[@"加载" S]]];
    CGSize size = CGSizeMake(font + 2, font + 2);
    UIGraphicsBeginImageContextWithOptions(size, false, 0);
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:NSMakeRange(0, attributedString.length)];
    textView.attributedText = attributedString;
    textView.linkTextAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor],
                                     NSUnderlineColorAttributeName: [UIColor lightGrayColor],
                                     NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
    textView.delegate = self;
    textView.editable = NO;
    textView.scrollEnabled = NO;
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    if ([[URL scheme] isEqualToString:@"LoadingAgain"]) {
        [self didClickAgainButton];
        return NO;
    }
    return YES;
}
@end
